package com.shuda.system.controller;

import com.shuda.common.dto.PageDTO;
import com.shuda.common.dto.PageResult;
import com.shuda.common.result.Result;
import com.shuda.system.dto.TenantDTO;
import com.shuda.system.dto.TenantQueryDTO;
import com.shuda.system.service.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 租户管理控制器
 */
@RestController
@RequestMapping("/api/system/tenant")
@Tag(name = "租户管理", description = "租户管理相关接口")
@RequiredArgsConstructor
@Validated
public class TenantController {

    private final TenantService tenantService;

    /**
     * 获取租户分页列表
     */
    @PostMapping("/page")
    @Operation(summary = "获取租户分页列表")
    public Result<PageResult<TenantDTO>> getTenantPage(@Valid @RequestBody PageDTO pageDTO, TenantQueryDTO queryDTO) {
        PageResult<TenantDTO> result = tenantService.getTenantPage(pageDTO, queryDTO);
        return Result.success(result);
    }

    /**
     * 根据ID获取租户详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取租户详情")
    public Result<TenantDTO> getTenantById(@PathVariable Long id) {
        TenantDTO tenantDTO = tenantService.getTenantById(id);
        return Result.success(tenantDTO);
    }

    /**
     * 根据租户编码获取租户详情
     */
    @GetMapping("/code/{tenantCode}")
    @Operation(summary = "根据租户编码获取租户详情")
    public Result<TenantDTO> getTenantByCode(@PathVariable String tenantCode) {
        TenantDTO tenantDTO = tenantService.getTenantByCode(tenantCode);
        return Result.success(tenantDTO);
    }

    /**
     * 创建租户
     */
    @PostMapping
    @Operation(summary = "创建租户")
    public Result<Long> createTenant(@Valid @RequestBody TenantDTO tenantDTO) {
        Long id = tenantService.createTenant(tenantDTO);
        return Result.success(id);
    }

    /**
     * 更新租户
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新租户")
    public Result<String> updateTenant(@PathVariable Long id, @Valid @RequestBody TenantDTO tenantDTO) {
        tenantService.updateTenant(id, tenantDTO);
        return Result.success("租户更新成功");
    }

    /**
     * 删除租户
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除租户")
    public Result<String> deleteTenant(@PathVariable Long id) {
        tenantService.deleteTenant(id);
        return Result.success("租户删除成功");
    }

    /**
     * 批量删除租户
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除租户")
    public Result<String> batchDeleteTenant(@RequestBody List<Long> ids) {
        tenantService.batchDeleteTenant(ids);
        return Result.success("租户批量删除成功");
    }

    /**
     * 更新租户状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新租户状态")
    public Result<String> updateTenantStatus(@PathVariable Long id, @RequestParam Integer status) {
        tenantService.updateTenantStatus(id, status);
        return Result.success("租户状态更新成功");
    }

    /**
     * 获取所有启用的租户列表
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有启用的租户列表")
    public Result<List<TenantDTO>> getAllEnabledTenants() {
        List<TenantDTO> tenants = tenantService.getAllEnabledTenants();
        return Result.success(tenants);
    }
}